package com.copa.l74;

/**
 * @author copa
 * @createDate 2022-06-24 09:07
 * @function 二分查找法
 */
public class Test1 {

    public boolean searchMatrix(int[][] matrix, int target) {
        int left, right;
        for (int i = 0; i < matrix.length; i++) {
            // 遍历行
            left = 0;
            right = matrix[i].length - 1;
            if (matrix[i][left] <= target && matrix[i][right] >= target) {
                // 如果在此行区间内，则进一步遍历此列
                // 二分查找法
                int mid;
                while (left <= right) {
                    mid = left + (right - left) / 2;
                    if (matrix[i][mid] == target) {
                        return true;
                    }
                    if (matrix[i][mid] > target) {
                        right = mid - 1;
                        continue;
                    }
                    left = mid + 1;
                }
                return false;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        //int[][] matrix = {{1}};
        //int[][] matrix = {{1,3}};
        //int[][] matrix = {{1},{3}};
        System.out.println(new Test1().searchMatrix(matrix, 3));
    }
}
